//
//  SZLSinglePayViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/10.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLSinglePayViewController.h"
#import "SinglePayDetailViewCell.h"
#import "SinglePayNetwork.h"
#import "MBProgressHUD+MJ.h"
#import "SZLAccountHelper.h"
#import "SZLSingleListViewController.h"
#import "PayTool.h"
#import "UIViewController+RTRootNavigationController.h"
#import "NewPaySuccessVc.h"

@interface SZLSinglePayViewController ()<UITableViewDataSource,UITableViewDelegate,PayTypeTableViewCellDelegate,UIAlertViewDelegate>

@property (nonatomic,strong) UITableView *sourceTab;
@property (nonatomic,assign) PayType payType;//支付类型

@end

@implementation SZLSinglePayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"收银台";
    self.payType = 5;//什么也没选
    [self setTableView];
}
//重写返回按钮方法
//- (UIBarButtonItem *)customBackItemWithTarget:(id)target action:(SEL)action
//{
//    UIButton *backBtn = [[UIButton alloc]init];
//    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back_pressed"] forState:UIControlStateNormal];
//    [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back_pressed"] forState:UIControlStateHighlighted];
//    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
//    [backBtn sizeToFit];
//    backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
//    return [[UIBarButtonItem alloc]initWithCustomView:backBtn];
//}
//- (void)backClick
//{
//    [self.navigationController popToRootViewControllerAnimated:YES];
//}
- (void)setTableView
{
    [self.view addSubview:self.sourceTab];
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 60)];
    UIButton *payBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, 20, ScreenW-30, 40)];
    [payBtn setTitle:@"支付" forState:UIControlStateNormal];
    [payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [payBtn setBackgroundImage:GetImage(@"btn_bg_login_normal") forState:UIControlStateNormal];
    [payBtn addTarget:self action:@selector(payBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:payBtn];
    self.sourceTab.tableFooterView = footerView;
}
- (void)payBtnClick
{
    if ((self.payType!=PayTypeAlipay)&&(self.payType!=PayTypeWeixin)&&(self.payType!=PayTypeLeftMoney)) {
        [MBProgressHUD showError:@"请选择支付方式" toView:self.view];
        return;
    }
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"payNumber"] = self.model.payNumber;
    switch (self.payType) {
        case PayTypeAlipay:
        {
            [self alipayTopay:parame];
        }
            break;
        case PayTypeWeixin:
        {
            [self weixinToPay:parame];
        }
            break;
        case PayTypeCard:
        {
            [self cardTopay:parame];
        }
            break;
        case PayTypeLeftMoney:
        {
            [self leftMoneyTopay:parame];
        }
            break;
        default:
            break;
    }
}
#pragma mark  ------------- 切换支付方式---------------
- (void)payTypeTableViewCellClick:(PayType)type
{
    self.payType = type;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.payChargeType == PayChargeTypeCharge||self.payChargeType == PayChargeTypeAdd) {
        return 168;
    }
    return 280;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PayTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PayTypeTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.model = self.model;
    cell.clipsToBounds = YES;
    return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(void)viewDidLayoutSubviews
{
    if ([self.sourceTab respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.sourceTab setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.sourceTab respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.sourceTab setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (UITableView *)sourceTab
{
    if (!_sourceTab) {
        _sourceTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) style:UITableViewStyleGrouped];
        _sourceTab.delegate = self;
        _sourceTab.dataSource = self;
        _sourceTab.backgroundColor = RGBColorOne(240);
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([SinglePayDetailViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SinglePayDetailViewCell class])];
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([PayTypeTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([PayTypeTableViewCell class])];
        _sourceTab.tableFooterView = [[UIView alloc]init];
    }
    return _sourceTab;
}
#pragma mark --------------------支付相关-------------------------------
- (void)leftMoneyTopay:(NSDictionary *)parame
{
    if (self.model.userAccountBalance<self.model.sumPrice) {
        [MBProgressHUD showError:@"余额不足" toView:self.view];
        return;
    }
    __WEAK
    [[PayTool shareTool] leftMoneyWithParame:parame CallBack:^{
        [weakSelf jumpToSuccess];
    }];
}

- (void)alipayTopay:(NSDictionary *)parame
{
    __WEAK
    [[PayTool shareTool] aplipayWithParame:parame callback:^() {
        [weakSelf jumpToSuccess];
    }];
}
- (void)cardTopay:(NSDictionary *)parame
{
    __WEAK
    [[PayTool shareTool]cardPayWithParame:parame callback:^{
        [weakSelf jumpToSuccess];
    }];
}

- (void)weixinToPay:(NSDictionary *)parame
{
    __WEAK
    [[PayTool shareTool]weixinPayWithParame:parame callback:^{
        [weakSelf jumpToSuccess];
    }];
}

- (void)jumpToSuccess
{
//    if (self.paySuccessBlock) {
//        self.paySuccessBlock();
//    }
    __WEAK
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.payChargeType == PayChargeTypeAdd) {
//            [self.navigationController popToRootViewControllerAnimated:YES];
            NewPaySuccessVc *vc = [[NewPaySuccessVc alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }else {
            
            SZLSingleListViewController *successVc = [[SZLSingleListViewController alloc]init];
            successVc.model = weakSelf.model;
            successVc.type = SingleTypeReadyDeliver;
            successVc.isFromPay = YES;
            [weakSelf.navigationController pushViewController:successVc animated:YES];
        }
    });
}

@end
